package cn.hwadee.muying.controller.sys;


import cn.hwadee.muying.commons.base.BaseController;
import cn.hwadee.muying.commons.jqgrid.GridResult;
import cn.hwadee.muying.commons.page.MyPage;
import cn.hwadee.muying.entity.Permission;
import cn.hwadee.muying.service.PermissionService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Tony
 * @since 2018-04-17
 */
@Controller
@RequestMapping("/sys/perm")
public class PermController extends BaseController {

    @Autowired
    private PermissionService permService;

    @ModelAttribute("perm")
    public Permission get(@RequestParam(required = false, value = "id") Long id) throws Exception {
        if (id != null && id > 0) {
            return permService.selectById(id);
        } else {
            return new Permission();
        }
    }

    //@RequiresPermissions("sys:perm:list")
    @RequestMapping(value = "/list", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String list() throws Exception{
        return "/sys/perm/list";
    }

    @ResponseBody
    //@RequiresPermissions("sys:perm:list")
    @RequestMapping(value = "/list.json", produces = "application/json;charset=utf-8")
    public GridResult list(@RequestParam(name = "pid",defaultValue = "0") Long pid) throws Exception{
        MyPage<Permission> page = getPage();
        page.setRecords(permService.selectSubperms(pid));
        GridResult result = new GridResult(page.getCurrent(),page.getPages(),page.getCurrent(),page.getRecords());
        return result;
    }

    //@RequiresPermissions("sys:perm:edit")
    @RequestMapping(value = "/add", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String add(@ModelAttribute("perm") Permission perm, Model model) throws Exception{
        if(perm.getId()==null){
            perm.setPid(0L);
        }else{
            perm.setPid(perm.getId());
        }
        perm.setId(null);
        perm.setName(null);
        model.addAttribute("perm",perm);
        return "/sys/perm/edit";
    }

    //@RequiresPermissions("sys:perm:edit")
    @RequestMapping(value = "/edit", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String edit(@ModelAttribute("perm") Permission perm, Model model) throws Exception{
        model.addAttribute("perm",perm);
        return "/sys/perm/edit";
    }


    /**
     * 更新权限／菜单信息
     * @param perm
     * @return
     * @throws Exception
     */
    //@RequiresPermissions("sys:perm:edit")
    @RequestMapping(value = "/edit", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute("perm") Permission perm){
        Map<String,Object> result = new HashedMap();
        try {
            if(perm.getId() != null && perm.getId() > 0){
                permService.updateById(perm);
            }else {
                permService.insertPerm(perm);
            }
            result.put("status",200);
            result.put("msg","权限／菜单信息保存成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","权限／菜单信息保存失败");
        }
        return result;
    }

    /**
     * 删除权限／菜单
     * @param id
     * @return
     * @throws Exception
     */
    //@RequiresPermissions("sys:perm:delete")
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashedMap();
        try {
            if(id != null && id > 0){
                permService.deletePerm(id);
                result.put("status",200);
                result.put("msg","删除权限／菜单成功");
            }else {
                result.put("status",500);
                result.put("msg","请选择删除的权限／菜单");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","删除权限／菜单失败");
        }
        return result;
    }
}

